#pragma once
#include "net_interface.hpp"
#include <hash_map>
#include <map>

class CPacketParser
    :public IPacketParser
{
public:
    virtual INT32 ParsePacket(const char* pData, const INT32 dwLen)
    {
        if (dwLen < sizeof(UINT32))
        {
            return 0;
        }

        INT32 dwPkglen = *(INT32*)pData;

        if (dwPkglen > dwLen)
        {
            return 0;
        }

        return dwPkglen;
    };
protected:
private:
};

class CClient;
typedef std::map<HSESSION, CClient*> CClientMap;

//typedef stdext::hash_map<H, CClient*> ClientHashMap;

class CClientMgr
    :public ISessionManager
{
public:
    CClientMgr(void);
    virtual ~CClientMgr(void);

    bool Start(const char* pszIP, UINT16 wPort, INT32 iCount, INT32 iPkgSize);
    void Stop(void);
    static unsigned __stdcall ThreadFunc(void* pVoid);

    virtual void OnEstablish(HNETHANDLE hHandle, HSESSION hSession);
    virtual void OnTerminate(HSESSION hSession);
    virtual void OnError(HNETHANDLE hHandle, HSESSION hSession, INT32 iModuleError, INT32 iSystemError);
    virtual void OnRecv(HSESSION hSession, const char* pData, const INT32 iLen);

protected:
    bool __Init(void);
    void __UnInit(void);
    void __OnExecute(void);
    void __CheckClient(void);

public:
    HMODULE			m_hNetDll;
    INetManager*	m_poNetMgr;
    HANDLE			m_hThread;
    bool			m_bTerminate;
    CPacketParser	m_oPacketParser;
    CClient*        m_pArryClient;
    CClientMap      m_mapClient;
//    ClientHashMap   m_HashMapClient;
    char            m_szIP[64];
    UINT16          m_wPort;
    INT32           m_iCount;
    INT32           m_iRecvBufSize;
    INT32           m_iSendBufSize;
    INT32           m_iPkgSize;

public:
    UINT32          m_dwConnectSuccess;
    UINT32          m_dwConnectFail;
    UINT32          m_dwConnectDelayTotal;
    UINT32          m_dwPkgSendTotal;
    UINT32          m_dwPkgRecvTotal;
    UINT64          m_qwByteSendTotal;
    UINT64          m_qwByteRecvTotal;
    UINT32          m_dwRecvDelayTotal;
    UINT32          m_dwRecvDelayMax;
    UINT32          m_dwStarSec;
    UINT32          m_dwErrorPkg;
};


//IDC_EDIT_PORT
//IDC_EDIT_PKGSIZE
//IDC_EDIT_SESSIONNUM
//IDC_EDIT_CONNECT_SUCCESS
//IDC_EDIT_CONNECT_FAIL
//IDC_EDIT_CONNECT_DELAY
//IDC_EDIT_CONNECT_AVERAGE
//IDC_EDIT_PKG_SEND_TOTAL
//IDC_EDIT_PKG_RECV_TOTAL
//IDC_EDIT_PKG_SEND_PERSEC
//IDC_EDIT_PKG_RECV_PERSEC
//IDC_EDIT_BYTE_SEND_PERSEC
//IDC_EDIT_BYTE_RECV_PERSEC
//IDC_EDIT_RECV_DELAY
//IDC_EDIT_RECV_DELAY_MAX
//IDC_EDIT_ERROR_PKG
